//
//  SafeCenterTableViewController.m
//  联付商圈
//
//  Created by 韩佳睿 on 2017/7/19.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "SafeCenterTableViewController.h"
#import "ModifyPasswordViewController.h"
#import "ChangePhoneNumViewController.h"
#import "PaidNumViewController.h"
//#import "PersonalCell.h"
#import "CYPasswordView.h"

@interface SafeCenterTableViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) CYPasswordView *passwordView;
@property (nonatomic, strong)NSArray *arr;

@end

@implementation SafeCenterTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self prepareNavBar];
    [self prepareData];
    [self prepareTableView];
    [self addNotificationCenter];
}

- (void)prepareData {
    self.arr = @[@[@"设置支付密码"], @[@"重置登录密码", @"重置支付密码"]];
}

- (void)addNotificationCenter {
    [CYNotificationCenter addObserver:self selector:@selector(cancel) name:CYPasswordViewCancleButtonClickNotification object:nil];
    [CYNotificationCenter addObserver:self selector:@selector(forgetPWD) name:CYPasswordViewForgetPWDButtonClickNotification object:nil];
}

- (void)prepareNavBar{
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"安全中心";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

- (void)prepareTableView {
    self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section?2:1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            __weak SafeCenterTableViewController *weakSelf = self;
            self.passwordView = [[CYPasswordView alloc] init];
            self.passwordView.title = @"请设置支付密码";
            [self.passwordView showInView:self.view.window];
            
            self.passwordView.finish = ^(NSString *password) {
                
                [weakSelf.passwordView hideKeyboard];
                [weakSelf.passwordView startLoading];
                
                [weakSelf loadData:password];
            };
        }
//            else if (indexPath.row == 1) {
//            ChangePhoneNumViewController *vc = [ChangePhoneNumViewController new];
//            [self.navigationController pushViewController:vc animated:YES];
//        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            ModifyPasswordViewController *modifyPasswordVC = [ModifyPasswordViewController new];
            [self.navigationController pushViewController:modifyPasswordVC animated:YES];
          
        } else if (indexPath.row == 1) {
            PaidNumViewController *vc = [PaidNumViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (void)loadData:(NSString *)payword {
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
    SQUserModel *model = [SQUserModel yy_modelWithDictionary:dic];
    
    NSDictionary *param = @{@"name" : model.userName, @"payword" : payword};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    [manager GET:[Host stringByAppendingString:@"payword?"] parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if (dic[@"success"]) {
            
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
            HUD.labelText = dic[@"success"][@"message"];
            HUD.mode = MBProgressHUDModeText;
            [HUD hide:YES afterDelay:1.0];
        } else {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
            HUD.labelText = dic[@"error"][@"message"];
            HUD.mode = MBProgressHUDModeText;
            [HUD hide:YES afterDelay:1.0];
        }
        [self.passwordView hide];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

- (void)cancel {

}

- (void)forgetPWD {

}
@end
